
import { GCtrl } from "./util/GCtrl";
import { AgentType } from "./util/friendData";
import { DefaultData } from "./DefaultData";
import { TongXunLuData } from "./TongXunLuData";
import { GroupDetailData } from "./util/GroupDetailData";
import { GroupVerifyData } from "./util/GroupVerifyData";
import { NewFriendData } from "./util/NewFriendData";


const { ccclass, property } = cc._decorator;

@ccclass
export default class SelfData extends cc.Component {

    @property(cc.Prefab)
    memberHeadPrf: cc.Prefab = null;

    //-------------

    private serverNtcIndex: number = null;
    private eventIndexList: any = {};
    private requestFuncArr: Function[] = [];
    onLoad() {
        this.serverNtcIndex = cc.vv.eventCustom.on("UserSelfInfo", this.serverNtc.bind(this), this.node);
        this.eventIndexList['updatePlayerInfo'] = cc.vv.eventCustom.on('updatePlayerInfo', this.onPlayerCoinChanged.bind(this), this.node);
        this.eventIndexList['initSelfData'] = cc.vv.eventCustom.on('initSelfData', this.initSelfData.bind(this), this.node);
        this.eventIndexList['LoginSccess'] = cc.vv.eventCustom.on('LoginSccess', this.LoginSccess.bind(this), this.node);
        this.eventIndexList['logout'] = cc.vv.eventCustom.on('logout', this.logout.bind(this), this.node);
        cc.vv.eventCustom.on('loadAgentData', this.loadAgentData.bind(this), this.node);
        cc.vv.eventCustom.on('GetClubPermissions', this.GetClubPermissions.bind(this), this.node);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.keyboard, this);
        this.loadRequest();
    }
    start() {
        GCtrl.self = this;
        GCtrl.memberHeadPrf = this.memberHeadPrf;
        this.initSelfDataLocal();
        //test
        this.test();
    }
    onDestroy() {
        cc.vv.eventCustom.off("ServerNtc", this.serverNtcIndex);
        this.serverNtcIndex = null;
        this.eventIndexList['updatePlayerInfo'] && cc.vv.eventCustom.off('updatePlayerInfo', this.eventIndexList['updatePlayerInfo']);
        this.eventIndexList['initSelfData'] && cc.vv.eventCustom.off('initSelfData', this.eventIndexList['initSelfData']);
        this.eventIndexList['LoginSccess'] && cc.vv.eventCustom.off('LoginSccess', this.eventIndexList['LoginSccess']);
        this.eventIndexList['logout'] && cc.vv.eventCustom.off('logout', this.eventIndexList['logout']);
        cc.vv.eventCustom.off('loadAgentData', this.loadAgentData.bind(this), this);
        cc.vv.eventCustom.off('GetClubPermissions', this.GetClubPermissions.bind(this), this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.keyboard, this);
    }

    private serverNtc(data: any) {
        if (!data || !data[0]) {
            cc.warn('个人数据为空: ', data);
            return;
        }
        let self_data = data[0];
        GCtrl.dataConfig.SelfData = self_data;
        this.initSelfData();
        this.loadRequest();
        this.doRequest();
    }
    private initSelfData() {
        cc.vv.TongXunLuData = TongXunLuData.getInstance();
        cc.vv.TongXunLuData.init();
        cc.vv.NewFriendData = NewFriendData.getInstance();
        cc.vv.NewFriendData.init();

        cc.vv.GroupVerifyData = GroupVerifyData.getInstance();

        cc.vv.GroupDetailData = GroupDetailData.getInstance();
        cc.vv.GroupDetailData.init();

        if (GCtrl.dataConfig.SelfData) {
            //开始获取聊天历史数据
            cc.vv.chatMgr.startLoadOfflineMsg();
        }
    }
    private initSelfDataLocal() {
        // cc.vv.TongXunLuHaoYouData = TongXunLuHaoYouData.getInstance();
        // cc.vv.TongXunLuHaoYouData.init();
    }
    private loadAgentData() {
        cc.vv.GLUtils.sendRequest('GetIdentity', {}, function (err, data) {
            if (data.errorcode) {
                cc.vv.GLUtils.showTips(data.errordes);
            } else {
                let type: number = null;
                if (data.isAgent == 1) {
                    type = AgentType.regionalAgent;
                }
                else if (data.isAgent == 2) {
                    type = AgentType.normalAgent;
                }
                else if (data.inClub == true) {
                    type = AgentType.playerWithClub;
                }
                else if (data.isPlayer == true) {
                    type = AgentType.playerWithoutClub;
                }
                else if (data.isCusSvc == true) {
                    type = AgentType.service;
                }
                else {
                    type = AgentType.user;
                }
                (GCtrl.session.getParentRid() && type != AgentType.regionalAgent && type != AgentType.normalAgent)
                    && (type = AgentType.playerWithClub);
                cc.vv.session.setType(type);
                cc.vv.eventCustom.emit('updateIdentity', type);
            }
            this.doRequest();
        }.bind(this));
    }
    private getTransPassState() {
        cc.vv.GLUtils.sendRequest('CheckTransPass', {}, function (err, data) {
            if (data.errcodedes) {
                cc.vv.GLUtils.showTips(data.errcodedes);
            } else {
                GCtrl.dataConfig.isHaveTransPass = data.res;
            }
            this.doRequest();
        }.bind(this));
    }
    private getAliPayBankState() {
        cc.vv.GLUtils.sendRequest('PayBankAliData', {}, function (err, data) {
            if (data.errcodedes) {
                cc.vv.GLUtils.showTips(data.errcodedes);
            } else {
                cc.vv.session.setBank_number(data.bank_account_code);
                cc.vv.session.setRealName(data.bank_account_owner);
                cc.vv.session.setAlipay_number(data.alipay);
            }
            this.doRequest();
        }.bind(this));
    }
    private GetClubPermissions(data) {
        let callback = data && data[0];
        cc.vv.GLUtils.sendRequest('GetClubPermissions', {}, function (err, data) {
            if (data.errorcode) {
                cc.vv.GLUtils.showTips(data.errordes);
            } else {
                // let state = data.state || ApplyAgentState.NoPermission;
                // agentData.SetApplyAgentState(state);
                callback && callback(data);
            }
            this.doRequest();
        }.bind(this));
    }
    private onPlayerCoinChanged(data: any) {
        if (!data) return false;
        data = data[0];
        data.coin != void 0 && cc.vv.session.setCoin(data.coin);
        data.diamond != void 0 && cc.vv.session.setDiamond(data.diamond);
        cc.vv.eventCustom.emit('updatePlayerCoin', cc.vv.session.getCoin(), cc.vv.session.getDiamond());
        return true;
    }
    private putRequest(func: Function) {
        if (!func) return false;
        this.requestFuncArr.push(func);
        return true;
    }
    private getRequest() {
        if (this.requestFuncArr.length == 0) return null;
        let func = this.requestFuncArr.shift();
        return func;
    }
    private loadRequest() {
        this.putRequest(this.loadAgentData.bind(this));
        this.putRequest(this.getTransPassState.bind(this));
        this.putRequest(this.getAliPayBankState.bind(this));
        this.putRequest(this.GetClubPermissions.bind(this));
    }
    private doRequest() {
        let func = this.getRequest();
        func && func();
    }
    private LoginSccess() {
        //打开转菊花
        // cc.vv.eventCustom.emit("showloading")
        // this.scheduleOnce(() => {
        //     console.log("防御代码 把这个loading关掉")
        //     cc.vv.eventCustom.emit("hideloading")
        // }, 10)
    }
    private logout() {
        GCtrl.dataConfig.SelfData = null;
        GCtrl.session.setPlayer(DefaultData.originSelfInfo);
    }
    private keyboard(evt) {
        let code = evt.keyCode;
        switch (code) {
            case cc.macro.KEY.q: {
                cc.vv.GameChooseConfig.setMenu(cc.vv.GameChooseConfig.menu.chat);
                cc.vv.eventCustom.emit("chooseMenuItem");
            } break;
            case cc.macro.KEY.w: {
                cc.vv.GameChooseConfig.setMenu(cc.vv.GameChooseConfig.menu.addressBook);
                cc.vv.eventCustom.emit("chooseMenuItem");
            } break;
            case cc.macro.KEY.e: {
                cc.vv.GameChooseConfig.setMenu(cc.vv.GameChooseConfig.menu.club);
                cc.vv.eventCustom.emit("chooseMenuItem");
            } break;
            case cc.macro.KEY.r: {
                cc.vv.GameChooseConfig.setMenu(cc.vv.GameChooseConfig.menu.personal);
                cc.vv.eventCustom.emit("chooseMenuItem");
            } break;
            case 6: {//被阻挡的系统返回键值
                // return;
                // syskeyMgr.onClickSysKey_Back();
            } break;
        }
    }
    private test() {
    }
}
